package net.androy.smugmugtool;

import java.util.List;

import net.androy.smugmugtool.vo.SearchResult;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLHandler extends DefaultHandler{
	
	private boolean inItem = false;
	private String currentNode = "";
	private List<SearchResult> images;
	private String data = "";
	
	private SearchResult item;
	
	public XMLHandler(List images) {
		this.images = images;
	}
	
	
	
	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		if(localName.equals("item")){
			inItem = true;
			item = new SearchResult();
			
		}
		
		currentNode = localName;
	}
	
	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		
		
		if(localName.equals("item")){
			inItem = false;
			images.add(item);
		}
		
		if(localName.equals("guid")){
			if(data != null && data.trim().length() > 0){
				item.setGuid(data.trim());
				data = "";
			}
		}
//		else if(localName.equals("title")){
//			if(data != null && data.trim().length() > 0){
//				item.setTitle(data.trim());
//				data = "";
//			}
//		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if(inItem && currentNode != null && currentNode.equals("guid")){
			data += String.copyValueOf(ch, start, length);
		}
		
//		else if(inItem && currentNode != null && currentNode.equals("title")){
//			data += new String(ch, start, length);
//		}
	}
}
